package com.cutepeer.notes.databases;

import java.util.HashMap;

import com.google.android.gms.internal.fn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class NoteDatabaseHanler extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 2;

	// Database Name
	private static final String DATABASE_NAME = "android_note.db";

	//Table note
	private static final String TABLE_NOTE ="note";

	// Login Table Columns note
	private static final String KEY_ID = "noteId";
	private static final String KEY_UID = "noteUserid";
	private static final String KEY_NOTE_TEXT = "noteText";
	private static final String KEY_TIME = "noteTime";
	private static final String KEY_ALARM = "noteAlarm";
	private static final String KEY_SYNCH ="noteSynch";

	public NoteDatabaseHanler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_NOTE_TABLE = "CREATE TABLE " + TABLE_NOTE + "("
				+ KEY_ID + " INTEGER PRIMARY KEY,"
				+ KEY_UID + " INTEGER,"
				+ KEY_NOTE_TEXT + " TEXT UNIQUE,"
				+ KEY_TIME + " TEXT,"
				+ KEY_ALARM + " INTEGER,"
				+ KEY_SYNCH + " INTEGER" + ")";
		Log.e("v", CREATE_NOTE_TABLE);

		db.execSQL(CREATE_NOTE_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);

		// Create tables again
		onCreate(db);
	}

	/**
	 * Storing note details in database
	 * */
	public void addNote(String noteId, String noteUserid, String noteText, String noteTime,String noteAlarm, String noteSynch) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_ID, noteId); //Note id
		values.put(KEY_UID, noteUserid); // User id
		values.put(KEY_NOTE_TEXT, noteText); // text on note
		values.put(KEY_TIME, noteTime); // Time Note
		values.put(KEY_ALARM, noteAlarm);// Alarm note
		values.put(KEY_SYNCH, noteSynch);// Sys note



		// Inserting Row
		db.insert(TABLE_NOTE, null, values);
		//db.close(); // Closing database connection
	}

	/**
	 * Getting note data from database
	 * */
	public HashMap<String, String> getUserDetails(){
		HashMap<String,String> note = new HashMap<String,String>();
		String selectQuery = "SELECT  * FROM " + TABLE_NOTE;

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// Move to first row
		cursor.moveToFirst();
		if(cursor.getCount() > 0){
			note.put("noteId", cursor.getString(1));
			note.put("noteUserid", cursor.getString(2));
			note.put("noteText", cursor.getString(3));
			note.put("noteTime", cursor.getString(4));
			note.put("noteAlarm", cursor.getString(5));
			note.put("noteSynch", cursor.getString(5));


		}
		cursor.close();
		db.close();
		// return user
		return note;
	}

	/**
	 * Re crate database
	 * Delete all tables and create them again
	 * */

	public void resetTables(){
		SQLiteDatabase db = this.getWritableDatabase();
		// Delete All Rows
		db.delete(TABLE_NOTE, null, null);
		db.close();
	}

}